package com.ruoyi.fetv.util;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.Random;

/**
 * @ClassName Util
 * @Description
 * @Author 肖润杰
 * @Time 2023/3/20 13:53
 * @Version 1.0
 */

public class RandomBeanUtils {

    private static final Random random = new Random();

    public static void fillRandomValues(Object object) throws IllegalAccessException {
        Class<?> clazz = object.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            Class<?> fieldType = field.getType();
            if (fieldType.equals(int.class) || fieldType.equals(Integer.class)) {
                field.set(object, random.nextInt());
            } else if (fieldType.equals(long.class) || fieldType.equals(Long.class)) {
                field.set(object, random.nextLong());
            } else if (fieldType.equals(float.class) || fieldType.equals(Float.class)) {
                field.set(object, random.nextFloat());
            } else if (fieldType.equals(double.class) || fieldType.equals(Double.class)) {
                field.set(object, random.nextDouble());
            } else if (fieldType.equals(boolean.class) || fieldType.equals(Boolean.class)) {
                field.set(object, random.nextBoolean());
            } else if (fieldType.equals(String.class)) {
                int length = random.nextInt(10) + 1;
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < length; i++) {
                    sb.append((char) (random.nextInt(26) + 97));
                }
                field.set(object, sb.toString());
            } else {
                System.err.println(fieldType);
                throw new IllegalAccessException();
            }
        }
    }

    /**
     * 数字填充1
     * boolean填充 true
     * 字符填充很好
     */
    public static void fillValues(Object object) throws IllegalAccessException {
        Class<?> clazz = object.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            Class<?> fieldType = field.getType();
            if (fieldType.equals(int.class) || fieldType.equals(Integer.class)) {
                field.set(object, 1);
            } else if (fieldType.equals(long.class) || fieldType.equals(Long.class)) {
                field.set(object, 1L);
            } else if (fieldType.equals(float.class) || fieldType.equals(Float.class)) {
                field.set(object, 1f);
            } else if (fieldType.equals(double.class) || fieldType.equals(Double.class)) {
                field.set(object, 1d);
            } else if (fieldType.equals(boolean.class) || fieldType.equals(Boolean.class)) {
                field.set(object, true);
            } else if (fieldType.equals(String.class)) {
                field.set(object, "很好");
            } else if (fieldType.equals(Date.class)) {
                field.set(object, new Date());
            } else {
                System.err.println(fieldType);
                throw new IllegalAccessException();
            }
        }
    }
}
